<!DOCTYPE html>
<html lang="ZH-cn">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Login</title>
  <!-- build:css -->
  <link rel="stylesheet" href="../lib/layui/css/layui.css">
  <link rel="stylesheet" href="../css/login.css">
  <!-- endbuild -->
</head>

<body>
  <div class="kit-login">
    <div class="kit-login-bg"></div>
    <div class="kit-login-wapper">
      <h2 class="kit-login-slogan">欢迎使用 <br> VIP SYSTEM</h2>
      <div class="kit-login-form">
        <h4 class="kit-login-title">登录</h4>
        <form class="layui-form">
          <div class="kit-login-row">
            <div class="kit-login-col">
              <i class="layui-icon">&#xe612;</i>
              <span class="kit-login-input">
                <input type="text" name="username" lay-verify="required" placeholder="用户名" />
              </span>
            </div>
            <div class="kit-login-col"></div>
          </div>
          <div class="kit-login-row">
            <div class="kit-login-col">
              <i class="layui-icon">&#xe64c;</i>
              <span class="kit-login-input">
                <input type="password" name="password" lay-verify="required" placeholder="密码" />
              </span>
            </div>
            <div class="kit-login-col"></div>
          </div>
          <div class="kit-login-row">
            <button class="layui-btn kit-login-btn" lay-submit="submit" lay-filter="login_hash">登录</button>
          </div>
          <div class="kit-login-row" style="margin-bottom:0;">
            <a href="javascript:;" style="color: rgb(153, 153, 153); text-decoration: none; font-size: 13px;"
              id="forgot">忘记密码</a>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- build:js -->
  <script src="../lib/polyfill.min.js"></script>
  <script src="../lib/layui/layui.js"></script>
  <script src="../mockjs/mockjs-config.js"></script>
  <!-- endbuild -->

  <script>
    //'axios', 'lodash'
    layui.config({
      base: '/js/'
    }).use(['layer', 'form', 'axios', 'jquery', 'lodash', 'api'], function () {
      var layer = layui.layer,
        form = layui.form,
        axios = layui.axios,
        $ = layui.jquery,
        api = layui.api,
        _ = layui.lodash;

      $('#forgot').on('click', function () {
        layer.msg('请联系管理员.');
      });

      //监听提交
      form.on('submit(login_hash)', function (data) {
        // var layIndex = layer.load(2, {
        //   shade: [0.1, '#393D49']
        // });
        console.log(data.field);
        axios.post(api.user.login, data.field)
          .then(response => {
            var data = response.data;
            if (data.success === true) {
              // 登陆成功
              var token = data.msg;
              localStorage.setItem('X-TOKEN', token);
              location.href = '/';
            } else {
              layer.msg('登陆失败');
            }
          });

        // request interceptor
        // axios.post('/api/v1/login', data.field)
        //   .then(function (response) {
        //     if (response.success === false) {
        //       //throw new Error(response.msg);
        //     }
        //     return response;
        //   })
        //   .then(function (res) {
        //     setTimeout(function () {
        //       if (res.success) {
        //         var to = getParams(location.href);
        //         //location.href = to !== null && to.ReturnUrl !== undefined ? unescape(to.ReturnUrl) : '/';
        //       } else {
        //         layer.msg(res.msg);
        //         layer.close(layIndex);
        //       }
        //     }, 500);
        //   })
        //   .catch(function (err) {
        //     console.log(err);
        //     layer.close(layIndex);
        //   });
        return false;
      });
    });

    function getParams(href) {
      var p = href.substr(href.indexOf('?') + 1);
      if (href === p) return null;
      var params = p.split('&');
      var data = {};
      _.forEach(params, function (item, index) {
        var kv = item.split('=');
        var key = kv[0];
        var value = kv[1];
        data[key] = value;
      });
      return data;
    }
  </script>
</body>

</html>